﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class EmpChangeAdd : System.Web.UI.Page
    {
        EmployeeBiz employeeBiz = new EmployeeBiz();
        EmpChangeBiz empChangeBiz = new EmpChangeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string changeID = "0";//By Update
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["ChangeID"] != null)
            {
                changeID = Request.Params["ChangeID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                //ddlChangeTypeName
                if (changeID != "0")
                    BindData(changeID);
                else
                {
                    defDicBiz.InitDropDownList("Admin_Change_type", ddlChangeTypeName, "");
                    defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlDutyID, "0");
                    defDicBiz.InitDropDownList("Admin_Work_Area", ddlAreaID, "0");
                    defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID, "0");
                    new DepartmentBiz().InitDropDownList(ddlDeptID, "0");//针对一层结构使用
                    txtChangeTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                }
                JSUtility.AddConfirm(btnDelete, "您真的要删除该职位变动的信息吗？");             
                

            }
        }
        private void BindData(string changeID)
        {
            DataTable dt = empChangeBiz.GetOneInfo(changeID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtOldDeptName.Text = dt.Rows[0]["OldDeptName"].ToString();
                txtOldDutyName.Text = dt.Rows[0]["OldDutyName"].ToString();
                txtOldAreaName.Text = dt.Rows[0]["OldAreaName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                txtTestTimeLong.Text = dt.Rows[0]["TestTimeLong"].ToString();
                defDicBiz.InitDropDownList("Admin_Change_type", ddlChangeTypeName, dt.Rows[0]["ChangeTypeName"].ToString());
               
                defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlDutyID, dt.Rows[0]["NewDutyName"].ToString());
                
                new DepartmentBiz().InitDropDownList(ddlDeptID, dt.Rows[0]["NewDeptName"].ToString());
                defDicBiz.InitDropDownList("Admin_Work_Area", ddlAreaID, dt.Rows[0]["NewAreaName"].ToString());
                defDicBiz.InitDropDownList("Admin_Work_State", ddlStateID, dt.Rows[0]["NewStateName"].ToString());
                string ChangeTime = dt.Rows[0]["ChangeTime"].ToString();
                if (Utils.RegexRule.IsDate(ChangeTime))
                    txtChangeTime.Text = Convert.ToDateTime(ChangeTime).ToString("yyyy-MM-dd");

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                            txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();

                            defDicBiz.InitSynDropDownList(ddlDutyID, dt.Rows[0]["DutyName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlDeptID, dt.Rows[0]["DeptName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlAreaID, dt.Rows[0]["AreaName"].ToString());
                            defDicBiz.InitSynDropDownList(ddlStateID, dt.Rows[0]["StateName"].ToString());
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职(复职之后才能操作): " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                        txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                        defDicBiz.InitSynDropDownList(ddlDutyID, dt.Rows[0]["DutyName"].ToString());
                        defDicBiz.InitSynDropDownList(ddlDeptID, dt.Rows[0]["DeptName"].ToString());
                        defDicBiz.InitSynDropDownList(ddlAreaID, dt.Rows[0]["AreaName"].ToString());
                        defDicBiz.InitSynDropDownList(ddlStateID, dt.Rows[0]["StateName"].ToString());
                    }
                    else
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpChangeAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpChangeList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "您没有增加权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();           
            string changeTime = txtChangeTime.Text.Trim();            
            string testTimeLong = txtTestTimeLong.Text.Trim(); 
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("NewStateName", ddlStateID.SelectedItem.Text.Trim());
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlDutyID.SelectedItem.Text.Trim());
            sdList.Add("NewDeptName", StringUtility.RemoveLeftSpace(ddlDeptID.SelectedItem.Text.Trim()));
            sdList.Add("NewAreaName", ddlAreaID.SelectedItem.Text.Trim());
            sdList.Add("ChangeTypeName", ddlChangeTypeName.SelectedItem.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("TestTimeLong", testTimeLong);
            
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            //更改员工部门
            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("EmpCode", txtEmpCode.Text.Trim());
            hList.Add("DutyID", ddlDutyID.SelectedValue);
            hList.Add("DeptID", ddlDeptID.SelectedValue);
            hList.Add("AreaID", ddlAreaID.SelectedValue);
            hList.Add("StateID", ddlStateID.SelectedValue);
            string info = empChangeBiz.Insert( sdList, hList); 
            JSUtility.AlertAndRedirect(info, "EmpChangeAdd.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpChangeAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpChangeList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();          
            string changeTime = txtChangeTime.Text.Trim();           
            string testTimeLong = txtTestTimeLong.Text.Trim();
            
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ChangeID", changeID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("NewStateName", ddlStateID.SelectedItem.Text.Trim());
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlDutyID.SelectedItem.Text.Trim());
            sdList.Add("NewDeptName", StringUtility.RemoveLeftSpace(ddlDeptID.SelectedItem.Text.Trim()));
            sdList.Add("NewAreaName", ddlAreaID.SelectedItem.Text.Trim());
            sdList.Add("ChangeTypeName", ddlChangeTypeName.SelectedItem.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("TestTimeLong", testTimeLong);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("EmpCode", txtEmpCode.Text.Trim());
            hList.Add("DutyID", ddlDutyID.SelectedValue);
            hList.Add("DeptID", ddlDeptID.SelectedValue);
            hList.Add("AreaID", ddlAreaID.SelectedValue);
            hList.Add("StateID", ddlStateID.SelectedValue);
            if (changeID != "0")
            {
                string info = empChangeBiz.Update(sdList, hList);                
                JSUtility.Alert(this, info);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpChangeAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpChangeList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除权限！");
                    return;
                }
            }
            if (changeID != "0")
            {
                if (empChangeBiz.DeleteOneInfo(changeID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }

       

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}